<template>
  <el-container class="home-container">
    <el-header>
        <div>
            <img src="../assets/heima.png" alt="">
            <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click='logout'>注销</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <!-- 侧边栏菜单区域 -->
        
    <!--  unique-opened 是否只保持一个子菜单的展开
          collapse 是否水平折叠收起菜单
          collapse-transition : 是否开启折叠动画 
    -->
        <div class="toggle-button" @click="isBtn">|||</div>
        <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="pink"
      unique-opened
      :collapse = "isCollapse"
      :collapse-transition = "false"
      router
      :default-active="activePath" >
      <!-- 一级菜单 -->
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">

        <!-- 一级菜单的模板区域 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="iconslist[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
          <el-menu-item :index="'/'+subItem.path"
           @click="PathActive('/'+subItem.path)"
            v-for="subItem in item.children"
             :key="subItem.id">
            <template slot="title">
          <!-- 图标 -->
          <i class="el-icon-menu"></i>
          <!-- 文本 -->
          <span >{{subItem.authName}}</span>
        </template>
          </el-menu-item>
      </el-submenu>
     
    </el-menu>

      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return{
      isCollapse:false ,
      activePath:'',
      menulist:[],
      iconslist:{
        '125':'iconfont icon-users',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'

      }
    }
  },
  created(){
    this.getMenuList()
  },
  methods: {
    //注销
    logout() {
      //删除token
      window.sessionStorage.clear()
      //跳转回登录页
      this.$router.push('login')
    },
    //获取列表内容
    async getMenuList(){
      const {data:res} = await this.$http.get('menus')
      //判断是否获取成功
      if(res.meta.status != 200) return this.$message.error(res.meta.msg)
      //把获取到的内容赋值
      this.menulist = res.data
    },
    //判断是否收起菜单
    isBtn(){
      this.isCollapse = !this.isCollapse
    },
    PathActive(active){
      window.sessionStorage.getItem("activePath",active)
      this.activePath = active
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
    height: 100%;
}
.el-header{
    background-color: #3eabf3;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;//按钮居中
    color:#fff;
    font-size:20px;
    >div{
        display:flex;
        justify-items: center;
        span{
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333744;
    .el-menu{
      border-right:none;
    }
}
.el-main{
    background-color: #eaedf1
}
.iconfont{
  margin-right: 10px;
}

.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
